package org.jeecg.modules.demo.com.kunpeng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.demo.com.kunpeng.entity.ContactsDetails;
import org.jeecg.modules.demo.com.kunpeng.entity.CustomerInformation;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.CustomerInformationUpdateDTO;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.CustomerQueryListDTO;
import org.jeecg.modules.demo.com.kunpeng.service.ICustomerInformationService;
import org.jeecg.modules.demo.com.kunpeng.vo.CustomerInformationPage;
import org.jeecg.modules.demo.com.kunpeng.vo.CustomerInformationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: 其他合同表
 * @Author: jeecg-boot
 * @Date:   2022-12-23
 * @Version: V1.0
 */
@Api(tags="客户表")
@RestController
@RequestMapping("/com.kunpeng/customerInformation")
@Slf4j
public class CustomerInformationController {

    @Autowired
    private ICustomerInformationService customerInformationService;

    /**
     * 获取客户列表
     */
    @ApiOperation(value="客户表-获取客户列表", notes="客户表-获取客户列表")
    @PostMapping("/queryCustomerList")
    public Result<IPage<CustomerInformationVO>> queryCustomerList(@RequestBody CustomerQueryListDTO customerQueryListDTO,
                                                                    @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
        log.info("开始处理获取客户列表请求...");
        Result<Page<CustomerInformationVO>> result = new Result<Page<CustomerInformationVO>>();
        Page<CustomerInformationVO> pageList = new Page<CustomerInformationVO>(pageNo,pageSize);
        pageList = customerInformationService.list(pageList,customerQueryListDTO);//通知公告消息
        if(pageList.getRecords().size()==0){
            return Result.error("没有符合条件的数据，请重新输入！");
        }
        log.info("查询当前页："+pageList.getCurrent());
        log.info("查询当前页数量："+pageList.getSize());
        log.info("查询结果数量："+pageList.getRecords().size());
        log.info("数据总数："+pageList.getTotal());
        result.setSuccess(true);
        result.setResult(pageList);
        return Result.ok(pageList);
    }

    /**
     * 获取废弃客户列表
     * @return
     */
    @ApiOperation(value="客户表-获取废弃客户列表", notes="客户表-获取废弃客户列表")
    @PostMapping("/queryDiscardCustomerList")
    public Result<IPage<CustomerInformationVO>> queryDiscardCustomerList(@RequestBody CustomerQueryListDTO customerQueryListDTO,
                                                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
        log.info("开始处理获取客户列表请求...");
        Result<Page<CustomerInformationVO>> result = new Result<Page<CustomerInformationVO>>();
        Page<CustomerInformationVO> pageList = new Page<CustomerInformationVO>(pageNo,pageSize);
        pageList = customerInformationService.discardCustomerList(pageList,customerQueryListDTO);//通知公告消息
        if(pageList.getRecords().size()==0){
            return Result.error("没有符合条件的数据，请重新输入！");
        }
        log.info("查询当前页："+pageList.getCurrent());
        log.info("查询当前页数量："+pageList.getSize());
        log.info("查询结果数量："+pageList.getRecords().size());
        log.info("数据总数："+pageList.getTotal());
        result.setSuccess(true);
        result.setResult(pageList);
        return Result.ok(pageList);
    }

    @ApiOperation(value="客户表-获取客户名称", notes="客户表-获取客户名称")
    @PostMapping("/customerNameList")
    public Result<List<String>> customerNameList(){
        log.info("开始处理获取客户名称列表请求...");
        List<String> customerNameList = customerInformationService.listCustomerName();
        for (String customerName:customerNameList) {
            System.out.println("客户名："+customerName);
        }
        return Result.ok(customerNameList);
    }

    /**
     *   添加
     * @param customerInformationPage
     * @return
     */
    @AutoLog(value = "客户信息表-添加")
    @ApiOperation(value="客户信息表-添加", notes="客户信息表-添加")
    //@RequiresPermissions("com.kunpeng:customer_information:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody CustomerInformationPage customerInformationPage) {
        System.out.println("customerInformationPage:"+customerInformationPage.toString());

        customerInformationService.insert(customerInformationPage);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     * @param customerInformationUpdateDTO
     * @return
     */
    @AutoLog(value = "客户信息表-编辑")
    @ApiOperation(value="客户信息表-编辑", notes="客户信息表-编辑")
    //@RequiresPermissions("com.kunpeng:customer_information:edit")
    @PostMapping(value = "/edit")
    public Result<String> edit(@RequestBody CustomerInformationUpdateDTO customerInformationUpdateDTO) {
        customerInformationService.updateById(customerInformationUpdateDTO);
        return Result.OK("编辑成功!");
    }

    /**
     *  通过id删除
     * @param id
     * @return
     */
    @AutoLog(value = "客户信息表-通过id删除")
    @ApiOperation(value="客户信息表-通过id删除", notes="客户信息表-通过id删除")
    //@RequiresPermissions("com.kunpeng:customer_information:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        customerInformationService.deleteById(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     * @param ids
     * @return
     */
    @AutoLog(value = "客户信息表-批量删除")
    @ApiOperation(value="客户信息表-批量删除", notes="客户信息表-批量删除")
    //@RequiresPermissions("com.kunpeng:customer_information:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.customerInformationService.deleteBatch(ids.split(","));
        return Result.OK("批量删除成功!");
    }

}
